{{define "cp_reddit"}}

{{template "cp_head" .}}
<header class="page-header">
    <h2>Reddit feeds</h2>
</header>

{{template "cp_alerts" .}}

<div class="row">
    <div class="col">
        <div class="tabs">
            <ul class="nav nav-tabs">
                <li class="nav-item active"><a class="nav-link show active" href="#fast-feeds"
                        aria-controls="fast-feeds" role="tab" data-toggle="tab">
                        Fast / Simple
                    </a></li>
                <li class="nav-item"><a class="nav-link" href="#slow-feeds" aria-controls="slow-feeds" role="tab"
                        data-toggle="tab">
                        Slow / Advanced
                    </a></li>
            </ul>
            <div class="tab-content">
                <div role="tabpanel" class="tab-pane active" id="fast-feeds">
                    {{mTemplate "reddit_feed_tab_content" "Dot" . "Slow" false}}</div>
                <div role="tabpanel" class="tab-pane" id="slow-feeds">
                    {{mTemplate "reddit_feed_tab_content" "Dot" . "Slow" true}}</div>
            </div>
        </div>
    </div>
</div>

{{template "cp_footer" .}}

{{end}}

{{define "reddit_feed_tab_content"}}
<!-- /.row -->
<div class="row mb-5 pb-2">
    <div class="col-md-6">
        <h3>{{if .Slow}}Slow{{else}}Fast{{end}} reddit feeds</h3>
        <p>Reddit feeds will post posts from subreddits in your discord server, the fast feed is ratelimited at 300
            posts per hour and the slow feed is 200.</p>
        <p>{{if .Slow}}<b>Slow</b> feeds are 15 minutes behind so that you can apply score filters to them to only post
            popular posts in your discord. Useful for big subs. If you have it set to 10 it means that if the post has a
            score of above 10 after 15 minutes, it will be posted in your discord
            {{else}}<b>Fast</b> feeds are 1 minute behind and you can't apply upvote filters to them, use the slow feed
            for that.{{end}}</p>
        <p>The subreddit field is just the name of the subreddit (not the URL), examples: "games",
            "multicopter"</p>
        <p><b>If Server Channel is set to "None" the added or already active Reddit feed will be disabled.</b></p>
    </div>
    <div class="col-md-6">
        <h3>New feed</h3>
        <form method="post" action="/manage/{{.Dot.ActiveGuild.ID}}/reddit" data-async-form>
            {{if .Slow}}<input type="text" class="hidden" name="slow" value="1">{{end}}
            <div class="form-row">
                <div class="form-group col">
                    <label for="new-subreddit-slow-{{.Slow}}">Subreddit</label>
                    <input type="text" class="form-control" id="new-subreddit-slow-{{.Slow}}" name="subreddit"
                        placeholder="discord_irl">
                </div>
                <div class="form-group col">
                    <label for="new-channel-slow-{{.Slow}}">Server Channel</label>
                    <select id="new-channel-slow-{{.Slow}}" class="form-control" name="channel">
                        {{textChannelOptions .Dot.ActiveGuild.Channels nil false ""}}
                    </select>
                </div>
            </div>
            <div class="form-row mb-3">
                <div class="form-group col">
                    <label>NSFW Filtering</label>
                    <select name="nsfw_filter" class="form-control">
                        <option value="0">None</option>
                        <option value="1">Ignore NSFW Posts</option>
                        <option value="2">Only post NSFW Posts</option>
                    </select>
                </div>
                {{if .Slow}}<div class="form-group col">
                    <label>Minimum needed score</label>
                    <input type="number" min="0" name="min_upvotes" value="5" class="form-control">
                </div>{{end}}
            </div>

            {{checkbox "spoilers_enabled" (printf "spoiler-toggle-new-slow-%t" .Slow) `Spoilers enabled<small class="ml-2">(On Reddit posts marked as spoilers)</small>` true}}
            {{checkbox "use_embeds" (printf "embed-new-slow-%t" .Slow) `Use embeds<small class="ml-2">(Videos won't be attached, but just linked)</small>` true}}

            <button type="submit" class="btn btn-success">Add</button>
        </form>
    </div> <!-- col -->
</div> <!-- row -->

<h3>Current reddit feeds</h3>
{{$guild := .Dot.ActiveGuild.ID}}
{{$channels := .Dot.ActiveGuild.Channels}}
{{$slow := .Slow}}
{{range .Dot.RedditConfig}}{{if eq .Slow $slow}}
<form id="feed-item-{{.ID}}" data-async-form method="post" action="/manage/{{$guild}}/reddit/{{.ID}}/update">
    <div class="row border-bottom border-secondary pb-3">
        <div class="col-lg">
            <div class="form-row">
                <input type="text" class="hidden" name="id" value="{{.ID}}">
                <div class="form-group col">
                    <label>Subreddit</label>
                    <p class="form-control-static"><a class="feedlink" href="https://reddit.com/r/{{.Subreddit}}" target="_blank">r/{{.Subreddit}}</a></p>
                </div>
                <div class="form-group col">
                    <label for="channel-feed-{{.ID}}">Server Channel</label>
                    <select id="channel-feed-{{.ID}}" class="form-control" name="channel">
                        {{textChannelOptions $channels .ChannelID false ""}}
                    </select>
                </div>
            </div>
        </div>
        <div class="col-lg">
            <div class="form-row">
                <div class="form-group col">
                    <label>NSFW Filtering</label>
                    <select name="nsfw_filter" class="form-control">
                        <option value="0" {{if eq .FilterNSFW 0}}selected{{end}}>None</option>
                        <option value="1" {{if eq .FilterNSFW 1}}selected{{end}}>Ignore NSFW Posts</option>
                        <option value="2" {{if eq .FilterNSFW 2}}selected{{end}}>Only post NSFW Posts</option>
                    </select>
                </div>
                {{if .Slow}}<div class="form-group col">
                    <label>Minimum needed score</label>
                    <input type="number" min="0" name="min_upvotes" value="{{.MinUpvotes}}" class="form-control">
                </div>{{end}}
            </div>
        </div>
        <div class="col-lg">
            <div class="form-row">
                <div class="col d-flex flex-column">
                    <Span class="mb-2">Enable Spoilers</span>
                    {{checkbox "spoilers_enabled" (print "spoiler-toggle-" .ID) `` .SpoilersEnabled}}
                </div>
                <div class="col d-flex flex-column">
                    <Span class="mb-2">Use embed</span>
                    {{checkbox "use_embeds" (print "use-embed-" .ID) `` .UseEmbeds}}
                </div>
                <div class="col d-flex flex-column">
                    <Span class="mb-2">Enabled</span>
                    {{checkbox "feed_enabled" (print "feed-enabled-" .ID) `` (not .Disabled)}}
                </div>

                <div class="form-group col">
                    <div class="btn-group mt-4">
                        <button form="feed-item-{{.ID}}" type="submit" class="btn btn-success ml-sm-3"
                            formaction="/manage/{{$guild}}/reddit/{{.ID}}/update">Save</button>
                        <button form="feed-item-{{.ID}}" type="submit" class="btn btn-danger"
                            formaction="/manage/{{$guild}}/reddit/{{.ID}}/delete">Delete</button>
                    </div>
                </div>
            </div>
        </div>
        <!-- /.col-lg-12 -->
    </div>
</form>
<!-- /.row -->
{{end}}{{end}}
{{end}}
